home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: basic question about pointers and local vars
- Date: 16 Feb 1996 22:30:41 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb16153041@qcd.lanl.gov>
- References: <genecutl-1602961021230001@dharma.hip.berkeley.edu>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: genecutl@mendel.berkeley.edu's message of 16 Feb 1996 18:18:18 GMT
-
- In article <genecutl-1602961021230001@dharma.hip.berkeley.edu>
- genecutl@mendel.berkeley.edu (gc) writes:
- <snip>
- main() {
-
- It is better style to write `int main (void)' or at least `int
- main()', though `main()' is sufficient.
-
- char *string;
- function(string);
- ...
-
- If you had just filled up the ..., we could all have helped
- you. Never, never, assume that the problem is where you think it is:
- show a small complete example.
-
- }
- function(char *string) {
- string = (char *)malloc(...);
-
- Calling malloc before declaring it results in undefined
- behaviour. Include <stdlib.h> at the top.
-
- Calling it with ... is a syntax error, of course :-)
-
- string[0] = 'a';
- string[1] = 'b';
- }
-
- It doesn't seem like it should be because the value of the pointer itself
- gets changed
- here and I would have thought that that gets lost when control goes back
- to main, but
-
- Very good. Correct logic.
-
- it does work when I try it. So where is the problem and what is the right
- way to do this?
-
- And some bad logic. What do you mean by when you try it? If you do
- something that is meaningless, very often the system will try to make
- the best sense of it: that may or may not be what you expected. It is
- wrong when you expect it, it is wrong when you don't. (I could have
- been more specific if you had told us in what way it met with your
- expectations, by posting a complete program.)
-
- The correct way involves using pointer to pointer, or returning a
- pointer.
-
- And read the FAQ before you post. It is very useful for beginner's
- questions: as well as many advanced ones too.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-